package com.qdu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qdu.bean.Task;
import com.qdu.bean.User;
import com.qdu.commons.AccessToken;
import com.qdu.util.EncryptUtil;
import com.qdu.commons.Result;
import com.qdu.service.TaskService;
import com.qdu.service.UserService;
import com.qdu.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    TaskService taskService;
    @Autowired
    UserService userService;

    /*该部分是生成任务*/
    @RequestMapping("/mt")
    public Result<String> makeTask(@RequestBody List<Task> taskList , HttpServletRequest servletRequest){
        String token = servletRequest.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = userService.getUser(username);
        taskService.makeTask(taskList, user.getCompany());
        return Result.success("添加成功");

    }

    /*跳转校验，只允许定制一个任务一个公司*/
    @RequestMapping("/ct")
    public Result<String> checkTask(HttpServletRequest servletRequest) {
        String token = servletRequest.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = userService.getUser(username);
        boolean isFinish = taskService.checkTask(user.getCompany());
        if (isFinish){
            return Result.success("可以进入");
        }
        return Result.failed("任务尚未完成");
    }
    /*展示任务*/
    @RequestMapping("/showtask")
    public Result<List<Task>> showTask(HttpServletRequest servletRequest){
        String token = servletRequest.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = userService.getUser(username);
        List<Task> taskList = taskService.showService(user.getCompany());
        return Result.success(taskList);
    }
    /*完成任务步骤*/
    @RequestMapping("/donetask")
    public Result<String> doneTask(@RequestBody String id){
        taskService.doneTask(id.substring(0,id.indexOf("=")));
        return Result.success("success");
    }
    /*完成所有的任务步骤*/
    @RequestMapping("/donealltask")
    public Result<String> doneAllTask(@RequestBody String company){
        taskService.doneAllTask(company.substring(0,company.indexOf("=")));
        return Result.success("success");
    }




}
